<?php
namespace app\common\model;

use think\Model;
use think\Db;

class UserGroup extends Model
{
    protected $insert = ['create_time'];

    protected static function init(){
        parent::init();

        self::event('after_insert', function ($data){
            $d_name = isset($data['name']) ? $data['name'] : 'hello';
            file_put_contents('./requestMsg.log',time().'用户group粉丝表新增了数据=1=:'.$d_name."\n", FILE_APPEND);
        });

        self::event('after_update', function ($data){
            $d_name = isset($data['name']) ? $data['name'] : 'hello';
            file_put_contents('./requestMsg.log',time().'用户group粉丝表更新了数据=1=:'.$d_name."\n", FILE_APPEND);
        });

    }


    protected function setCreateTimeAttr(){
        return date('Y:m:d H:i:s');
    }

    public function getLabelTopClass($auid){
        $result = [];
        $label_result_arr = Db::name('user_group')->where('auid',$auid)->select();
        foreach($label_result_arr as $v){
            if($v['pid'] == 0){
                $v['s_label'] = [];
                foreach($label_result_arr as $vo){
                    if($v['id'] == $vo['pid']){
                        $v['s_label'][] = $vo;
                    }
                }
                $result[] = $v;
            }
        }
        return $result;
    }

    /**
     * 自动生成时间
     * @return bool|string
     */
    protected function setCreateTime()
    {
        return date('Y-m-d H:i:s');
    }

    //获取层级缩进列表数据
    public function getLevelList()
    {
        $tags_level = $this->order(['sort'=>'desc'])->where('is_delete',1)->select();
        return array2level($tags_level);
    }
}